/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.sample;

import org.webos.java.base.Application;
import org.webos.java.base.ApplicationType;
import org.webos.java.base.Session;
import org.webos.java.base.hsqldb.HSQLDBConnection;
import org.webos.java.base.hsqldb.migration.DBDefinition;
import org.webos.java.base.hsqldb.migration.RowDefinition;
import org.webos.java.base.hsqldb.migration.TableDefinition;
import org.webos.java.base.workspace.FrameHolder;
import org.webos.java.base.workspace.Splitter;
import org.webos.java.base.workspace.Workspace;
import org.webos.java.root.Log;

/**
 *
 * @author kostya
 */

@ApplicationType(name="Address Book", code="abook", jsClass="sample.Application",
    description="Simple Address Book application", icon="ab_icons ab_icon")
public class AddressBookApplication extends Application{

    MainFrame mainFrame = null;
    TreeFrame treeFrame = null;
    HSQLDBConnection connection = new HSQLDBConnection("/.addressbook/data/db", "sa", "");

    public AddressBookApplication(Session aSession) {
        super(aSession);
        if(!setupDB()){
            sendError(901, "Can't setup database");
        }
        Workspace ws = new Workspace(this, "sample-ws");
        ws.setCaption("Addressbook");
        Splitter sp = new Splitter(null, "main");
        sp.setVertical(true);
        sp.setRatio(0.3);
        FrameHolder root = new FrameHolder(sp, "ws");
        sp.setRightItem(root);
        FrameHolder left = new FrameHolder(sp, "left");
        sp.setLeftItem(left);
        ws.setItem(sp);
        mainFrame = new MainFrame(this, ws);
        root.addFrame(mainFrame);
        treeFrame = new TreeFrame(this, ws);
        left.addFrame(treeFrame);
        addWorkspace(ws);
        showWorkspace(ws);
        addFrame(treeFrame);
        addFrame(mainFrame);

        Workspace anotherWorkspace = new Workspace(this, "float");
        anotherWorkspace.setDirection(Workspace.POSITION_BOTTOM);
        anotherWorkspace.setType(Workspace.TYPE_FLOAT);
//        anotherWorkspace.setCollapsed(false);
        FrameHolder fh = new FrameHolder(null, "float");
        anotherWorkspace.setItem(fh);

        FloatFrame floatFrame = new FloatFrame(this, anotherWorkspace);
        fh.addFrame(floatFrame);
    }

    private boolean setupDB(){
        try {
            if(connection.openConnection(getSession())){
                DBDefinition db = new DBDefinition();
                TableDefinition groups = new TableDefinition("GROUPS");
                RowDefinition idEntry = new RowDefinition("ID", "INTEGER", true);
                groups.addRow(idEntry);
                groups.addRow(new RowDefinition("NAME", "VARCHAR(255)"));
                db.addTable(groups);
                TableDefinition entries = new TableDefinition("ENTRIES");
                entries.addRow(new RowDefinition("ID", "INTEGER", true));
                entries.addRow(new RowDefinition("LASTNAME", "VARCHAR(255)", "''"));
                entries.addRow(new RowDefinition("FIRSTNAME", "VARCHAR(255)", "''"));
                entries.addRow(new RowDefinition("EMAIL", "VARCHAR(255)", "''"));
                entries.addRow(new RowDefinition("WORK_EMAIL", "VARCHAR(255)", "''"));
                entries.addRow(new RowDefinition("WORK_PHONE", "VARCHAR(255)", "''"));
                entries.addRow(new RowDefinition("HOME_PHONE", "VARCHAR(255)", "''"));
                entries.addRow(new RowDefinition("CELL_PHONE", "VARCHAR(255)", "''"));
                entries.addRow(new RowDefinition("BIRTHDAY", "VARCHAR(20)", "''"));
                entries.addRow(new RowDefinition("NOTES", "VARCHAR(65536)", "''"));
                entries.addRow(new RowDefinition("ID_GROUP", "INTEGER", idEntry));
                db.addTable(entries);
                if(db.migrateDB(connection.getConnection())){
//                    connection.close();
                    return true;
                }
            }
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return false;
    }

    @Override
    public void terminate() {
        connection.close();
    }


}
